
# -*- coding: utf-8 -*-
from xTkinter import *
import tkFileDialog
import tkMessageBox as msgBox
from tkSimpleDialog import askstring
import sys
from Validation_Contraintes import *
Validateur = ValidationContrainte()
import xmlrpclib
class Vue_EditionRapport(Frame):
    def __init__(self,master,parent,**cnf):
        self.ListeChamps=[]
        self.ListEntries=[]
        self.ListLabels=[]
        self.ListeChampsCalculs = []
        self.ListeContraintes = []
        self.parent = parent
        self.master=master
        Frame.__init__(self,master,cnf)             
        self.pack(side=LEFT,anchor=NW)
        self.configure(width=1200,height=800)  
        self.Regle()
        self.ListeTables = []
        self.CBTables=xCombobox(self)
        self.CBTables.setLocation(25,25)
        self.CBTables.setWidth(15)
        self.CBTables.setListWidth(25)
        self.CBTables.setListHeight(5)
        self.obtenirTables() 
        self.CBTables.setValidator(self.obtenirChamps)
        self.CBTables.setText('<Choisir>')
        
        self.CBChamps=xCombobox(self)
        self.CBChamps.setLocation(140,25)
        self.CBChamps.setWidth(15)
        self.CBChamps.setListWidth(25)
        self.CBChamps.setListHeight(5)
        self.CBChamps.setValidator(None)
        self.CBChamps.setText('<Choisir>')
        
        self.CBJointureGauche=xCombobox(self)
        self.CBJointureGauche.setLocation(450,25)
        self.CBJointureGauche.setWidth(15)
        self.CBJointureGauche.setListWidth(40)
        self.CBJointureGauche.setListHeight(5)
        self.CBJointureGauche.setValidator(None)
        self.CBJointureGauche.setText('<Choisir>')
        
        self.CBJointureDroite=xCombobox(self)
        self.CBJointureDroite.setLocation(560,25)
        self.CBJointureDroite.setWidth(15)
        self.CBJointureDroite.setListWidth(40)
        self.CBJointureDroite.setListHeight(5)
        self.CBJointureDroite.setValidator(None)
        self.CBJointureDroite.setText('<Choisir>')
        
        self.CBChampCalcule=xCombobox(self)
        self.CBChampCalcule.setLocation(450, 55)
        self.CBChampCalcule.setWidth(15)
        self.CBChampCalcule.setListWidth(40)
        self.CBChampCalcule.setListHeight(5)
        self.CBChampCalcule.setValidator(None)
        self.CBChampCalcule.setText('<Choisir>')
        
        self.Bouton=xButton(self,text="Ajouter Champ",width=12, command=self.AjouterChamp)
        self.Bouton.setLocation(260,25)
        
        self.Bouton=xButton(self,text="Ajouter Jointure",width=14, command=self.AjouterJointure)
        self.Bouton.setLocation(675,25)
        
        self.Bouton=xButton(self,text="Creer Rapport",width=11, command=self.CreerRapport)
        self.Bouton.setLocation(355,25)
        
        self.Bouton=xButton(self,text="Dynamique",width=11, command=self.ChampCalculeDynamique)
        self.Bouton.setLocation(355,55)
        self.Bouton=xButton(self,text="Statique",width=11, command=self.ChampCalculeStatique)
        self.Bouton.setLocation(355,85)
        self.Bouton=xButton(self,text="Ajouter Operation",width=20, command=self.AjouterOperation)
        self.Bouton.setLocation(355,115)
        self.Bouton=xButton(self,text="Ajouter champ calculé",width=20, command=self.AjouterChampCalcule)
        self.Bouton.setLocation(355,145)
        
        self.CBOperation=xCombobox(self)
        self.CBOperation.setLocation(510, 115)
        self.CBOperation.setWidth(5)
        self.CBOperation.setListWidth(5)
        self.CBOperation.setListHeight(6)
        self.CBOperation.setValidator(None)
        Operation = ["+","/","-","*","(",")"]
        for op in Operation:
            self.CBOperation.addItem(op)
        
        self.ChampCalcul = xLabel(self,text="", width=50, anchor=SW )
        self.ChampCalcul.setLocation(510,145)
        
        self.ChampCalculStatique =xEntry(self,width=24)
        self.ChampCalculStatique.setLocation(450,85)

        self.TamponTable =  {}
        self.ListLabels = []
        self.ListEntries = []
        self.Champs = []
        self.Jointure = []
        self.Table = []
        self.index = 0
    
    
    def ChampCalculeStatique(self):
        text = self.ChampCalcul.getText() + self.ChampCalculStatique.getText()
        self.ChampCalcul.setText(text)
        
    def ChampCalculeDynamique(self):
        text = self.ChampCalcul.getText() + self.CBChampCalcule.getText()
        self.ChampCalcul.setText(text)
        
    def AjouterChampCalcule(self):
        text =  self.ChampCalcul.getText()
        self.ListLabels.append(xLabel(self,text=text, width=len(text)+2, anchor=SW ))
        self.ListLabels[len(self.ListLabels)-1].setLocation(25,(self.index+1)*25+25)
        self.ListeChampsCalculs.append(text)
        self.ListEntries.append(xEntry(self,width=24))
        self.ListEntries[len(self.ListEntries)-1].setLocation(175,(self.index+1)*25+25)
        self.ListEntries[len(self.ListEntries)-1].setText(text)
        self.index = self.index + 1
    
    def AjouterOperation(self):
        text = self.ChampCalcul.getText() + self.CBOperation.getText()
        self.ChampCalcul.setText(text)
        
    def CreerRapport(self):
        nomRapport = name = askstring("Creation de rapport", "Entrer le nom du rapport")
        print "creation dun rapport ", nomRapport
        Requete = "select "
        for Champs in self.Champs:
            Requete = Requete + Champs + ", "
        for Champs in self.ListeChampsCalculs:
            Requete = Requete + Champs + ", "
        Requete = Requete[:-2] + " from "
        for Table in self.Table:
            Requete = Requete + Table + ", "
        Requete = Requete[:-2] + " where 1=1 "
        if self.Jointure <> []:
            for Jointure in self.Jointure:
                Requete = Requete + " and "  + Jointure
        print Requete
        self.parent.serveur.InsererEnregistrement('PERP_RAPPORT', {'Nom' : nomRapport, 'Requete' : Requete })
        self.parent.serveur.DoCommit()
        self.parent.vue.menuRapport.add_command(label = nomRapport, command = lambda Requete=Requete : self.parent.vue.AfficherRapport(Requete), state = 'normal')
      
    def AjouterChamp(self):
        if self.TamponTable[self.CBTables.getText()] == []:
            return
        text = self.CBTables.getText() + "." + self.CBChamps.getText()
        self.CBJointureDroite.addItem(text)
        self.CBJointureGauche.addItem(text)
        self.CBChampCalcule.addItem(text)
        self.ListLabels.append(xLabel(self,text=text, width=len(text)+2, anchor=SW ))
        self.ListLabels[len(self.ListLabels)-1].setLocation(25,(self.index+1)*25+25)
        self.ListEntries.append(xEntry(self,width=24))
        self.ListEntries[len(self.ListEntries)-1].setLocation(175,(self.index+1)*25+25)
        self.ListEntries[len(self.ListEntries)-1].setText(self.CBChamps.getText())
        self.ListeChamps.append(self.CBTables.getText() + "." + self.CBChamps.getText())
        self.index = self.index + 1
        self.Champs.append(text)
        self.TamponTable[self.CBTables.getText()].remove(self.CBChamps.getText())
        if self.CBTables.getText() not in self.Table:
            self.Table.append(self.CBTables.getText());
        self.obtenirChamps()
        pass
    
    def AjouterJointure(self):
        text = self.CBJointureGauche.getText() + " = " + self.CBJointureDroite.getText()
        self.ListLabels.append(xLabel(self,text=text, width=len(text)+2, anchor=SW ))
        self.ListLabels[len(self.ListLabels)-1].setLocation(25,(self.index+1)*25+25)
        self.Jointure.append(self.CBJointureGauche.getText() + " = " + self.CBJointureDroite.getText())
        self.index = self.index + 1
        
    def obtenirTables(self):
        column=0 
        Reponse = self.parent.serveur.ObtenirListeTables(self.parent.user,self.parent.profil)
        if(Reponse[0]==1):
            self.ListeChamps = Reponse[1]
            for i in range(len(self.ListeChamps)):
                self.CBTables.addItem(self.ListeChamps[i][0])
        
    def obtenirChamps(self):
        self.CBChamps.clear()
        self.CBChamps.setListHeight(5)
        table=self.CBTables.getText()
        if self.TamponTable.has_key(self.CBTables.getText()):
            ListeChamp = self.TamponTable[table]
        else:
            Reponse = self.parent.serveur.ObtenirListeChamps(table)
            if(Reponse[0]==1):
                ListeChamp = []
                for Champs in Reponse[1]:
                    ListeChamp.append(Champs[0])
                self.TamponTable[table] = ListeChamp
                print self.TamponTable[table]
            else:
                msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
                return
        if ListeChamp == []:
             self.CBChamps.setText("vide")
        for Champs in ListeChamp:
            self.CBChamps.addItem(Champs)
        
        
    def close(self):
        self.destroy()
        pass

    def Regle(self):
        for x in range (800/25):
            X = x*25
            xLabel(self, text=str(X)).setLocation(X,0)
        for y in range (600/25):
            Y = y*25
            xLabel(self, text=str(Y)).setLocation(0,Y)
            
#class Controleur:
#    def __init__(self):
#        self.user = "ADMIN"
#        self.profil = "ADMIN"
#        Serveur='http://localhost:8000'
#        self.serveur=xmlrpclib.ServerProxy(Serveur)
#        root=Tk()
#        root.geometry('1200x600')
#        vi=Vue_EditionRapport(root,self)
#        root.mainloop()       
#
#Controleur = Controleur()

# -*- coding: utf-8 -*-
#from xTkinter import *
#import tkFileDialog
#import tkMessageBox as msgBox
#import sys
#from Validation_Contraintes import *
#Validateur = ValidationContrainte()
#import xmlrpclib
#class Vue_EditionRapport(Frame):
#    def __init__(self,master,parent,**cnf):
#        self.ListeChamps=[]
#        self.ListeTable = []
#        self.ListEntries=[]
#        self.ListLabels=[]
#        self.ListeContraintes = []
#        self.ListeJointures = []
#        self.parent = parent
#        self.master=master
#        Frame.__init__(self,master,cnf)             
#        self.pack(side=LEFT,anchor=NW)
#        self.configure(width=1600,height=1200)  
#        self.Regle()
#        self.ListeTables = []
#        self.CBTables=xCombobox(self)
#        self.CBTables.setLocation(25,25)
#        self.CBTables.setWidth(15)
#        self.CBTables.setListWidth(25)
#        self.CBTables.setListHeight(5)
#        self.obtenirTables() 
#        self.CBTables.setValidator(self.obtenirChamps)
#        self.CBTables.setText('<Choisir>')
#        
#        self.CBChamps=xCombobox(self)
#        self.CBChamps.setLocation(140,25)
#        self.CBChamps.setWidth(15)
#        self.CBChamps.setListWidth(25)
#        self.CBChamps.setListHeight(5)
#        self.CBChamps.setValidator(None)
#        self.CBChamps.setText('<Choisir>')
#        
#        self.CBJointureGauche=xCombobox(self)
#        self.CBJointureGauche.setLocation(450,25)
#        self.CBJointureGauche.setWidth(15)
#        self.CBJointureGauche.setListWidth(25)
#        self.CBJointureGauche.setListHeight(5)
#        self.CBJointureGauche.setValidator(None)
#        self.CBJointureGauche.setText('<Choisir>')
#        
#        self.CBJointureDroite=xCombobox(self)
#        self.CBJointureDroite.setLocation(560,25)
#        self.CBJointureDroite.setWidth(15)
#        self.CBJointureDroite.setListWidth(25)
#        self.CBJointureDroite.setListHeight(5)
#        self.CBJointureDroite.setValidator(None)
#        self.CBJointureDroite.setText('<Choisir>')
#        
#        
#        self.Bouton=xButton(self,text="Ajouter Champ",width=12, command=self.AjouterChamp)
#        self.Bouton.setLocation(260,25)
#        
#        self.Bouton=xButton(self,text="Ajouter Jointure",width=14, command=self.AjouterRapport)
#        self.Bouton.setLocation(675,25)
#        
#        self.Bouton=xButton(self,text="Ajouter Rapport",width=14, command=self.AjouterRapport)
#        self.Bouton.setLocation(850,25)
#        
#        self.TamponTable =  {}
#        self.ListLabels = []
#        self.ListEntries = []
#        self.Champs = []
#        self.index = 0
#        
#    def AjouterChamp(self):
#        self.Champs.append(self.CBTables.getText() + "." + self.CBChamps.getText())
#        self.TamponTable[self.CBTables.getText()].remove(self.CBChamps.getText())
#        text = self.CBTables.getText() + "." + self.CBChamps.getText()
#        if self.CBTables.getText() not in self.ListeTable:
#            self.ListeTable.append(self.CBTables.getText())
#        self.CBJointureDroite.addItem(text)
#        self.CBJointureGauche.addItem(text)
#        self.ListLabels.append(xLabel(self,text=text, width=len(text)+2, anchor=SW ))
#        self.ListLabels[len(self.ListLabels)-1].setLocation(25,(self.index+1)*25+25)
#        self.ListEntries.append(xEntry(self,width=24))
#        self.ListEntries[len(self.ListEntries)-1].setLocation(175,(self.index+1)*25+25)
#        self.ListEntries[len(self.ListEntries)-1].setText(self.CBChamps.getText())
#        self.index = self.index + 1
#        self.obtenirChamps()
#        pass
#    
#    def AjouterJointure(self):
#        pass
#    
#    def AjouterRapport(self):
#        Requete = "select "
#        for champ in self.Champs:
#            Requete = Requete + champ + ", "
#        Requete = Requete[:-2] + " from "
#        for table in self.ListeTable:
#            Requete = Requete + table + ", "
#        Requete = Requete[:-2]
#        print Requete
#        RequeteInsert = "insert into PERP_RAPPORT " + "'" + Requete + "'"
#        print RequeteInsert
#
#    
#    def obtenirTables(self):
#        column=0 
#        Reponse = self.parent.serveur.ObtenirListeTables()
#        if(Reponse[0]==1):
#            self.ListeChamps = Reponse[1]
#            for i in range(len(self.ListeChamps)):
#                self.CBTables.addItem(self.ListeChamps[i])
#        
#    def obtenirChamps(self):
#        self.CBChamps.clear()
#        self.CBChamps.setListHeight(5)
#        table=self.CBTables.getText()
#        if self.TamponTable.has_key(self.CBTables.getText()):
#            ListeChamp = self.TamponTable[table]
#        else:
#            Reponse = self.parent.serveur.ObtenirListeChamps(table)
#            if(Reponse[0]==1):
#                ListeChamp = []
#                for Champs in Reponse[1]:
#                    ListeChamp.append(Champs[0])
#                self.TamponTable[table] = ListeChamp
#                print self.TamponTable[table]
#            else:
#                msgBox.showerror('PERP 2009 | Édition des tables', 'Une erreur est survenue!\n'+Reponse[1])
#                return
#        if ListeChamp == []:
#             self.CBChamps.setText("vide")
#        for Champs in ListeChamp:
#            self.CBChamps.addItem(Champs)
#        
#        
#    def close(self):
#        self.destroy()
#        pass
#
#    def Regle(self):
#        for x in range (1600/25):
#            X = x*25
#            xLabel(self, text=str(X)).setLocation(X,0)
#        for y in range (1200/25):
#            Y = y*25
#            xLabel(self, text=str(Y)).setLocation(0,Y)
#            
#class Controleur:
#    def __init__(self):
#        Serveur='http://localhost:8000'
#        self.serveur=xmlrpclib.ServerProxy(Serveur)
#        root=Tk()
#        root.geometry('800x400')
#        vi=Vue_EditionRapport(root,self)
#        root.mainloop()       
#
#Controleur = Controleur()
#

